#include <string.h>
#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

class String{
public:
    String(const char * pstr)
    :_size(strlen(pstr))
    {
        if(_size > _capacity){
            _buffer._pointer = new char[strlen(pstr)+1]();
            strcpy(_buffer._pointer,pstr);
        }else{
            memset(_buffer._local,0,sizeof(_buffer._local));
            strcpy(_buffer._local,pstr);
        }
    }
    
    ~String(){
        if(_size > _capacity){
            delete [] _buffer._pointer;
            _buffer._pointer = nullptr;
        }
    }

    String(const String & rhs)
    :_size(rhs._size)
    {
        if(_size > _capacity){
            _buffer._pointer = new char[rhs._size+1]();
            strcpy(_buffer._pointer , rhs._buffer._pointer);
        }else{
            memset(_buffer._local,0,sizeof(_buffer._local));
            strcpy(_buffer._local,rhs._buffer._local);
        }
    }

    String & operator=(const String & rhs){
        if(this != &rhs){
            //处理左操作数
            if(_size > _capacity){
                delete [] _buffer._pointer;
            }else{
                memset(_buffer._local,0,sizeof(_buffer._local));
            }
            
            //处理右操作数
            if(rhs._size > rhs._capacity){
                _buffer._pointer = new char[rhs._size+1]();
                strcpy(_buffer._pointer,rhs._buffer._pointer);
            }else{
                strcpy(_buffer._local,rhs._buffer._local);
            }
        }
        _size = rhs._size;
        return *this;
    }

    size_t size() const{
        return _size;
    };

    char & operator[](size_t index){
        if(index >= _size){
            static char nullchar = '\0';
            return nullchar;
        }
        if(_size > _capacity){
            return _buffer._pointer[index];
        }else{
            return _buffer._local[index];
        }
    }

    friend ostream & operator<<(ostream & os,const String & rhs);
private:
    union Buffer{
        char * _pointer;
        char _local[16];
    };
    size_t _size;
    static const size_t _capacity = 15;

    Buffer _buffer;
};

ostream & operator<<(ostream & os,const String & rhs){
    if(rhs._size > rhs._capacity){
        os << rhs._buffer._pointer;
    }else{
        os << rhs._buffer._local;
    }
    return os;
}

void test(){

}

int main()
{
    test();
    return 0;
}

